<!-- 
	修改绑定的手机号码
 
 -->
<template>
	<view class="binding-box">
		<cu-custom :isBack="true" bgColor="bg-fa">
			<block slot="content">更换手机号码</block>
		</cu-custom>
		<view class="binding-title">
			<view class="title-left">
				<text class="iconfont iconanquan"></text>
			</view>
			<view class="title-right">
				<view class="text">
					为了您的账号安全
				</view>
				<view class="text">
					请验证要绑定的手机号
				</view>
			</view>
		</view>
		<view class="old-phone">
			当前绑定手机号:  {{oldPhone}}
		</view>
		
		<view class="form-box">
			<view class="input-label">
				<l-input   icon-font="32" incon-color="#E54D42" input-type="Number"
				placeholder-text="请输入手机号码"
				 :max-lenght="11" val-name="phone" @getVal="getVal"></l-input>
			</view>
			
			<view class="input-label u-margin-top-20">
				<view class="input-code-l">
					<l-input icon-font="32" incon-color="#E54D42" input-type="text"
						placeholder-text="请输入验证码"
					 :max-lenght="16" val-name="code" @getVal="getVal"></l-input>
				</view>
				<view class="input-code-r">
					<text class="code" @tap="getCode">{{codeText}}</text>
				</view>
			</view>
			
		</view>
		
		<view class="submit-btn">
			<u-button @tap="submitInfo" shape="circle" :ripple="true" :custom-style="{'background':'#FA436A','borderColor':'#FA436A','color':'#fff'}">确认修改</u-button>
		</view>
		
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {mapState} from "vuex"
	import {hasUserInfo} from "@/utils/hasLogin.js"
	import lInput from "@/components/input/lInput.vue"
	export default {
		name:"setPhone",
		data(){
			return {
				oldPhone:"",
				phone:"",
				code:"",
				codeText:"获取验证码",
				codeTime:120
			}
		},
		components:{
			lInput
		},
		computed:{
			...mapState("user",['userInfo'])
		},
		methods:{
			
			/*  提交信息  */
			submitInfo(){
				let phone = this.phone.trim()
				let regPhone = (/^1(3|4|5|6|7|8|9)\d{9}$/).test(phone)
				if(!regPhone){
					this.$refs.uToast.show({
						title: "手机号码格式不正确",
						duration:2000,
					})
					return false
				}
				if(phone == this.oldPhone){
					this.$refs.uToast.show({
						title: "新手机号码不能与旧的相同",
						duration:2000,
					})
					return false
				}
				if(this.code.trim().length == 0){
					this.$refs.uToast.show({
						title: "请填写验证码",
						duration:2000,
					})
					return false
				}
			},
			
			
			getVal(val,name){
				this[name] = val
			},
			getCode(){
				let t = setTimeout(()=>{
					clearTimeout(t)
					this.$refs.uToast.hide()
					if(this.codeTime !=120){
						return false
					}
					
					let regPhone = (/^1(3|4|5|6|7|8|9)\d{9}$/).test(this.phone)
					if(!regPhone){
						this.$refs.uToast.show({
							title: "手机号码格式不正确",
							duration:2000,
						})
						return false
					}
					if(this.phone == this.oldPhone){
						this.$refs.uToast.show({
							title: "新手机号码不能与旧的相同",
							duration:2000,
						})
						return false
					}
					this.codeTimeDown()
				},100)
			},
			/*  验证码时间倒计时  */
			codeTimeDown(){
				let ti = setInterval(()=>{
					if(this.codeTime == 0){
						this.codeTime = 120
						this.codeText = "获取验证码"
						clearInterval(ti)
						return false
					}
					this.codeTime -- 
					this.codeText = this.codeTime + 's后再次获取' 
				},1000)
			},
		},
		onLoad() {
			let has = hasUserInfo()
			if(has){
				this.oldPhone = this.userInfo.mobile
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.binding-box{
		.binding-title{
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			.title-left{
				padding-right: 20rpx;
				.iconfont{
					color: #bfbfbf;
					font-size: 200rpx;
				}
			}
			.title-right{
				.text{
					color: #333333;
					font-size: 28rpx;
				}
			}
		}
		.old-phone{
			color: #333333;
			font-size: 28rpx;
			text-align: center;
		}
		
		.form-box{
			background: #FFFFFF;
			margin-top: 50rpx;
			padding: 0 20rpx;
		}
		
		.input-label{
			padding-bottom: 10rpx;
			border-bottom: 1px solid #f1f1f1;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			.input-code-l{
				flex-grow: 1;
			}
			.input-code-r{
				width: 240rpx;
				padding: 0 10rpx;
				border-radius: 6rpx 6rpx;
				border: 2rpx solid #FA436A;
				background: #FA436A;
				flex-grow: 0;
				height: 70rpx;
				text-align: center;
				line-height: 70rpx;
				.code{
					font-size: 20rpx;
					color: #FFFFFF;
				}
			}
		}
		.submit-btn{
			width: 80%;
			margin-left: 10%;
			margin-top: 80rpx;
		}
	}
</style>
